package com.wanglei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanglei.domain.ResponseResult;
import com.wanglei.domain.dto.RoleDto;
import com.wanglei.domain.entity.Role;
import com.wanglei.domain.entity.RoleMenu;
import com.wanglei.domain.entity.UserRole;
import com.wanglei.domain.vo.PageVo;
import com.wanglei.domain.vo.RoleVo;
import com.wanglei.mapper.RoleMenuMapper;
import com.wanglei.mapper.UserRoleMapper;
import com.wanglei.service.RoleService;
import com.wanglei.mapper.RoleMapper;
import com.wanglei.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
* @author MrWang
* @description 针对表【sys_role(角色信息表)】的数据库操作Service实现
* @createDate 2023-07-10 16:15:53
*/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
    implements RoleService{

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public List<String> selectRolesByUserId(Long id) {
        //如果是超级管理员 返回admin即可
        if (id==1){
            List<String> roleKeys = new ArrayList<>();
            roleKeys.add("admin");
            return roleKeys;
        }
        //否则查询用户所具有的角色信息
        return roleMapper.selectRolesByUserId(id);
    }

    //角色列表
    @Override
    public ResponseResult roleList(Integer pageNum, Integer pageSize, String roleName, String status) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<Role>()
                .like(StringUtils.hasText(roleName),Role::getRoleName,roleName)
                .like(StringUtils.hasText(status),Role::getStatus,status);
        Page<Role> page = new Page<>(pageNum,pageSize);
        roleMapper.selectPage(page,queryWrapper);
        List<Role> roles = page.getRecords();
        List<RoleVo> roleVos = BeanCopyUtils.copyBeanList(roles, RoleVo.class);
        return ResponseResult.okResult(new PageVo(roleVos,page.getTotal()));
    }

    //修改角色状态
    @Override
    public ResponseResult changeStatus(RoleDto roleDto) {
        System.out.println("   000000000000000      "+roleDto);
        Role role = new Role();
        role.setId(Long.valueOf(roleDto.getRoleId()));
        role.setStatus(roleDto.getStatus());
        roleMapper.updateById(role);
        return ResponseResult.okResult();
    }

    //新增角色
    @Transactional
    @Override
    public ResponseResult addMenu(Role role) {
        //新增角色
        roleMapper.insert(role);
        //新增角色和菜单的关联信息
        List<String> menuIds = role.getMenuIds();
        for (int i = 0; i < menuIds.size(); i++) {
            roleMenuMapper.insert(new RoleMenu(role.getId(),Long.valueOf(menuIds.get(i))));
        }
        return ResponseResult.okResult();
    }

    //查看角色详情
    @Override
    public ResponseResult getRoleById(Long id) {
        Role role = roleMapper.selectById(id);
        return ResponseResult.okResult(role);
    }

    //更新角色信息
    @Transactional
    @Override
    public ResponseResult updateRole(Role role) {
        //更新Role
        roleMapper.updateById(role);
        //更新角色和菜单的关联信息 要先清空之前的关联（防止重复添加相同的关联）
        List<String> menuIds = role.getMenuIds();
        LambdaQueryWrapper<RoleMenu> queryWrapper = new LambdaQueryWrapper<RoleMenu>()
                .eq(RoleMenu::getRoleId,role.getId());
        roleMenuMapper.delete(queryWrapper);
        for (int i = 0; i < menuIds.size(); i++) {
            roleMenuMapper.insert(new RoleMenu(role.getId(), Long.valueOf(menuIds.get(i))));
        }
        return ResponseResult.okResult();
    }

    //删除角色
    @Transactional
    @Override
    public ResponseResult deleteRole(Long id) {
        //删除角色信息
        roleMapper.deleteById(id);
        //删除角色和菜单的关联信息
        LambdaQueryWrapper<RoleMenu> queryWrapper = new LambdaQueryWrapper<RoleMenu>()
                .eq(RoleMenu::getRoleId,id);
        roleMenuMapper.delete(queryWrapper);
        //删除角色和用户相关的信息
        LambdaQueryWrapper<UserRole> queryWrapper1 = new LambdaQueryWrapper<UserRole>()
                .eq(UserRole::getRoleId,id);
        userRoleMapper.delete(queryWrapper1);

        return ResponseResult.okResult();
    }

    //角色列表
    @Override
    public ResponseResult listAllRole() {
        List<Role> roles = roleMapper.selectList(null);
        return ResponseResult.okResult(roles);
    }

}




